/*
 * LinkSelection.java
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.admin.gui.map;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.geom.Rectangle2D;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.fpdev.core.Scenario;
import org.fpdev.core.basenet.BLink;
import org.fpdev.core.basenet.BNode;

/**
 * Collection of currently selected links in the MapPanel.
 * 
 * @author demory
 */
public class SelectedLinks extends HashSet<BLink> {
 
  private boolean visible_;
 
  public SelectedLinks() {
    super();
    visible_ = false;
  }
  
  public boolean isVisible() {
    return visible_;
  }
  
  public void setVisible(boolean v) {
    visible_ = v;
  }
  
  public void addFromIterator(Iterator<BLink> links) {
    while (links.hasNext()) add(links.next());
  }

  public void draw(MapCanvas canvas, Scenario scen) {
    canvas.setColor(Color.yellow);
    for (Iterator<BLink> links = iterator(); links.hasNext();) {
      BLink link = links.next();
      canvas.setStroke(new BasicStroke(link.getDrawWidth(canvas, scen) + 5, BasicStroke.CAP_ROUND, 0));
      link.drawCurve(canvas);
    }
  }

  public Set<BNode> getNodes() {
    Set<BNode> nodes = new HashSet<BNode>();
    for (Iterator<BLink> links = iterator(); links.hasNext();) {
      BLink link = links.next();
      nodes.add(link.getFNode());
      nodes.add(link.getTNode());
    }
    return nodes;
  }

  public Rectangle2D.Double getBoundingRect() {
    double minX = Double.MAX_VALUE, maxX = 0, minY = Double.MAX_VALUE, maxY = 0;
    for (Iterator<BLink> links = iterator(); links.hasNext();) {
      BLink link = links.next();
      if (link.getX1() < minX) {
        minX = link.getX1();
      }
      if (link.getX2() < minX) {
        minX = link.getX2();
      }
      if (link.getY1() < minY) {
        minY = link.getY1();
      }
      if (link.getY2() < minY) {
        minY = link.getY2();
      }
      if (link.getX1() > maxX) {
        maxX = link.getX1();
      }
      if (link.getX2() > maxX) {
        maxX = link.getX2();
      }
      if (link.getY1() > maxY) {
        maxY = link.getY1();
      }
      if (link.getY2() > maxY) {
        maxY = link.getY2();
      }
    }
    return new Rectangle2D.Double(minX, minY, maxX - minX, maxY - minY);
  }
}
